package lk;

public class D230912 {
    // 移除元素
    public static void main(String[] args) {
        new D230912().removeDuplicates(new int[]{0,0,1,1,1,2,2,3,3,4});
    }
    // 数组原地移除，val 的值
    public int removeElement(int[] nums, int val) {
        int length = nums.length;
        int count = 0;
        for (int i = 0; i < length - count;) {
            if (val == nums[i]) {
                int tmp = nums[i];
                nums[i] = nums[length - 1 - count];
                nums[length - 1 - count] = tmp;
                count++;
            } else {
                i++;
            }
        }
        return length - count;
    }

    // 删除有序数组中的重复项
    // 数组原地删除
    public int removeDuplicates(int[] nums) {
        int fast = 1;
        int slow = 1;

        while (fast < nums.length) {
            if (nums[fast] != nums[fast - 1]) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;
        }
        return slow;
    }


    // 删除有序数组中的重复项 II
    public int removeDuplicates2(int[] nums) {
        int n = nums.length;
        if (n <= 2) {
            return n;
        }
        int slow = 2, fast = 2;
        while (fast < n) {
            if (nums[slow - 2] != nums[fast]) {
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        return slow;
    }

    // 多数元素
    public int majorityElement(int[] array) {
        int ret = array[0];
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if(count == 0) {
                ret = array[i];
            }
            if (ret == array[i]) {
                count++;
            } else {
                count--;
            }
        }
        return ret;
    }
}
